<template>
  <a-card :loading="cardLoading" :bordered="false" style="height: 100%;">
    <a-spin :spinning="loading">
      <a-input-search @search="handleSearch" style="width:100%;margin-top: 10px" placeholder="输入小组名称查询..." enterButton />

      <a-tree
        showLine
        checkStrictly
        :expandedKeys.sync="expandedKeys"
        :selectedKeys="selectedKeys"
        :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
        :treeData="treeDataSource"
        :autoExpandParent="true"
        @select="handleTreeSelect"
      >
        <div slot="title" slot-scope="record">
          <span>{{ record.title }}</span>
          <span v-if="groupType == 'public'"> ( {{ record.groupDense_dictText }} ) </span>
        </div>
      </a-tree>
    </a-spin>
  </a-card>
</template>

<script>
  import { getAction } from '@/api/manage'
  import { mapGetters } from 'vuex'

  export default {
    name: 'GroupListLeft',
  props: ['value', 'groupType'],
    data() {
      return {
        cardLoading: true,
        loading: false,
        treeDataSource: [],
        selectedKeys: [],
        expandedKeys: [],
        intensiveType: '', // 密级
      }
    },
    created() {
      // this.commonRequestThen('')
    },
    methods: {
    ...mapGetters(['userInfo']),
      handleSearch(value) {
        this.commonRequestThen(value || '')
      },

      handleTreeSelect(selectedKeys, event) {
        if (selectedKeys.length > 0 && this.selectedKeys[0] !== selectedKeys[0]) {
          this.selectedKeys = [selectedKeys[0]]
          let orgCode = event.node.dataRef.id
          this.emitInput(orgCode)
        }
      },

      emitInput(id) {
        this.$emit('input', id)
      },

      commonRequestThen(value) {
        this.loading = true
        const params = {
          pageNo: 1,
          pageSize: -1,
          
          column: 'createTime',
          order: 'desc',
          groupName: value || '',
          groupType: this.groupType
        }
        if (!this.groupType || this.groupType == 'personal') {
          params.createBy = this.userInfo().username
        }
        if (this.intensiveType && this.intensiveType == 'secret') {
          params.groupDense = this.intensiveType
        }
        getAction('/api/erp/publish/erpPublishContactGroup/list', params).then(res => {
          if (res.success) {
            this.treeDataSource = res.result.records || []
            // update-begin- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
            // 默认选中第一条数据、默认展开所有第一级
            if (res.result.records.length > 0) {
              this.expandedKeys = []
              this.treeDataSource.forEach((item, index) => {
                item.title = item.groupName + ''
                item.key = item.id
                if (index === 0) {
                  this.selectedKeys = [item.id]
                  this.emitInput(item.id)
                }
                this.expandedKeys.push(item.id)
              })
            } else {
              this.emitInput('')
            }
           // update-end- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
          } else {
            this.$message.warn(res.message)
          }
        }).finally(() => {
          this.loading = false
          this.cardLoading = false
        })
      },

    }
  }
</script>

<style scoped lang="less">
/deep/ .ant-tree-node-content-wrapper{
  width: 100%;
  overflow: hidden !important;
  white-space: nowrap !important;
  text-overflow: ellipsis !important;
}
</style>